package com.dhx.web;

import com.dhx.pojo.LeaveStudent;
import com.dhx.pojo.User;
import com.dhx.service.InfoService;
import com.dhx.service.InfoServiceImpl;
import com.dhx.service.UserService;
import com.dhx.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class UserServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        InfoService infoService = new InfoServiceImpl();
        String action = req.getParameter("action");
        if ("login".equals(action)) {
            String id = req.getParameter("id");
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            User tempuser=new User(id, username, password, "admin");
            tempuser.setCate("admin");
            if (userService.login(tempuser) != null) {
                req.getSession().setAttribute("jiaoyan", username);
                List<LeaveStudent> infos = infoService.queryInfoes();
                req.setAttribute("info", infos);
                req.getRequestDispatcher("/pages/manager/managerleavestu.jsp").forward(req, resp);
            } else if (userService.login(new User(id, username, password, "user")) != null) {
                req.getSession().setAttribute("jiaoyan", username);
                List<LeaveStudent> infos = infoService.queryInfoes();
                req.setAttribute("info", infos);
                req.getRequestDispatcher("/pages/userview/userleavestu.jsp").forward(req, resp);
            } else {
                System.out.println("登陆失败！");
                resp.sendRedirect("/Student_Work/pages/user/login.jsp");
            }
        } else if ("regist".equals(action)) {
            String id = req.getParameter("id");
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            if (userService.existsUsername(id)) {
                System.out.println("用户名已存在" + username);
                req.setAttribute("msg", "用户名已存在！");
                req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
            } else {
                String cate = "user";
                userService.registUser(new User(id, username, password, cate));
                req.setAttribute("msg", "注册成功！请登录！");
                req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
            }
        }
    }
}
